<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Layouts {
	/**
	 * @var Objeto CodeIgniter
	 */
	var $CI;
	var $title;
	var $subtitle;
	
	var $js_includes = array();
	
  function Layouts()
  {
  	$this->CI =& get_instance();
		$this->title = null;
		$this->subtitle = null;
  }
	
	/**
	 * Carga una vista dentro de un layout.
	 * @return 
	 * @param object $view_name
	 * @param object $params[optional]
	 */
	function view($view_name, $params = array(), $layout = 'default') {
		if($params === null) {
			$params = array();
		}
		$view_content = $this->CI->load->view($view_name, $params, true);
		
		if($this->title !== null) {
			$this->title = '| ' . $this->title;
		}
		
		//ob_start('ob_gzhandler');
		//$this->CI->output->cache(15);
		$this->CI->load->view('layouts/' . $layout, array('content_for_layout' => $view_content, 'title_for_layout' => $this->title, 'title_for_h3' => $this->subtitle));
	}
	
	function show_404() {
		//$this->set_title('Página no encontrada');
		//$this->view('404');
		$this->CI->load->view('404');
	}
	
	/**
	 * Setea el titulo de la pagina.
	 * @return 
	 * @param object $title
	 */
	function set_title($title) {
		$this->title = $title;
	}
	
	/**
	 * Setea el subtitulo de la pagina
	 * @return 
	 * @param object $subtitle
	 */
	function set_subtitle($subtitle) {
		$this->subtitle = $subtitle;
	}
	
	/**
	 * Agrega includes de javascript al layout
	 *
	 * @param string $js 
	 * @return void
	 * @author Ian Murray
	 */
	function add_js($js, $append_path = TRUE)
	{
	  if($append_path) {
	    $path = base_url(); 
	  }
	  else {
	    $path = '';
	  }
	  
	  if(substr($js, -1, 3) != '.js') {
	    $js .= '.js';
	  }
	  
	  if(!is_array($js)) {
	    /* Assume its a string */
	    $this->js_includes[] = $path . $js;
	  }
	  else {
	    $this->js_includes += $path . $js;
	  }
	}
	
	/**
	 * Imprime los include de javascript. Para ser llamado desde 
	 *
	 * @return void
	 * @author Ian Murray
	 */
	function print_js_includes()
	{
	  $string = "";
	  foreach($this->js_includes as $js) {
	    $string .= '<script type="text/javascript" src="' . $js . '"></script>';
	  }
	  
	  return $string;
	}

	
	/**
	 * Redirecciona al usuario a la pagina dada.
	 *
	 * @param string $url 
	 * @param string $message 
	 * @param string $type 
	 * @return void
	 * @author Ian Murray
	 */
	/*function flash_redirect($url = '/', $message = '', $type = 'message')
	{
	  if($message != '') {
	    $this->CI->session->set_flashdata('message', $message);
      $this->CI->session->set_flashdata('type', $type);
    }
    
    if($this->CI->config->item('layouts_auto_redirect')) {
      redirect($url);
    }
    else {
      if($message != '') {
        $this->CI->session->keep_flashdata('message');
        $this->CI->session->keep_flashdata('type');
      }
      
      $this->view('flash_redirect', array('url' => $url));
    }
	}*/
	function flash_redirect($url = '/', $message = '', $type = 'message')
	{
	  if($message != '') {
	    $this->CI->flash->set_flashdata('message', $message);
      $this->CI->flash->set_flashdata('type', $type);
    }
    
    if($this->CI->config->item('layouts_auto_redirect')) {
      redirect($url);
    }
    else {
      //if($message != '') {
      //  $this->CI->flash->keep_flashdata('message');
      //  $this->CI->flash->keep_flashdata('type');
      //}
      
      $this->view('flash_redirect', array('url' => $url));
    }
	}
}
